package com.hamilton.erik.pt.mobile.android.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hamilton.erik.pt.core.model.Note;
import com.hamilton.erik.pt.mobile.android.R;

public class NoteListAdapter extends BaseAdapter {

	private List<Note> notes;
	private LayoutInflater layoutInflater;
	
	public NoteListAdapter(Context context, List<Note> notes) {
		this.notes = notes;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return notes.size();
	}

	@Override
	public Object getItem(int index) {
		return notes.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NoteHolder noteHolder;
		
		if(convertView == null) {
			convertView = layoutInflater.inflate(R.layout.note_row_view, null);
			
			noteHolder = new NoteHolder();
			
			noteHolder.date = (TextView)convertView.findViewById(R.id.note_date);
			noteHolder.title = (TextView)convertView.findViewById(R.id.note_title);
			noteHolder.note = (TextView)convertView.findViewById(R.id.note_note);
			
			convertView.setTag(noteHolder);
		}
		else {
			noteHolder = (NoteHolder)convertView.getTag();
		}
		
		noteHolder.date.setText(notes.get(position).getDate());
		noteHolder.title.setText(notes.get(position).getTitle());
		noteHolder.note.setText(notes.get(position).getNote());
		
		return convertView;
	}

	static class NoteHolder {
		TextView title;
		TextView note;
		TextView date;
	}

}
